package jee.boot.module.bpm.delegated;

import jee.boot.common.basic.Page;
import jee.boot.common.utils.JsonMapper;
import jee.boot.common.utils.StringUtils;
import jee.boot.entity.bpm.BusinessData;
import jee.boot.entity.task.IexpTaskConfig;
import jee.boot.entity.task.IexpTask;
import jee.boot.entity.task.IExportModel;
import jee.boot.entity.task.AbstractGeneralTemplateExportService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;


@Service
public class BusinessDataExport extends BasicDelegated implements AbstractGeneralTemplateExportService {
    private Logger logger = LoggerFactory.getLogger(getClass());


    /**
     * 根据task查询当前导出任务需要的数据,
     *
     * @param task 当前任务
     * @return 返回数据约束为Map类型
     */
    @Override
    public Map<String, Object> queryData(IexpTask task) {
        Map<String, Object> map = new HashMap<>();
        String jsonParam = task.getJsonParam();
        BusinessData businessData = new BusinessData();
        if (StringUtils.isNotBlank(jsonParam)) {
            try {
                businessData = JsonMapper.fromJsonString(jsonParam, BusinessData.class);
            } catch (Exception e) {
                logger.warn("解析参数失败,使用默认参数", e);
            }
        }
        Page<Map<String, Object>> page = businessDataService.findPage(businessData);
        map.put("list", page.getList());
        try {
            Thread.sleep(1000*20);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return map;
    }

    @Override
    public IexpTaskConfig taskConfig(IexpTask task) {
        IexpTaskConfig config = new IexpTaskConfig();
        String jsonParam = task.getJsonParam();
        BusinessData businessData = new BusinessData();
        if (StringUtils.isNotBlank(jsonParam)) {
            try {
                businessData = JsonMapper.fromJsonString(jsonParam, BusinessData.class);
            } catch (Exception e) {
                logger.warn("解析参数失败,使用默认参数", e);
            }
        }
        IExportModel exportModel = getExportModel(businessData,"exp");
        config.initExportByModelConfig(exportModel, exportModel.getTitle());
        return config;
    }

    public static void main(String[] args) {
        String a = "name";
        String[] split = StringUtils.split(a, ".");
        System.out.println(split[0]);
    }
}
